/****************************************************************************
 *
 *  LinuxChatty
 *  Copyright (C) 2010, David Hsu.
 * 
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 ***************************************************************************/
#include "Window.h"
#include <ncurses.h>

/**
 * Constructor
 * @param info A WindowInfo structure containing the width, height, and position
 */
Window::Window(WindowInfo& info):
	windowInfo(info)
{
	// Create the ncurses window
	window = newwin(info.height, info.width, info.topRow, info.leftCol);
}

/**
 * Virtual Destructor
 * Cleans up the created window
 */
Window::~Window()
{
	if (window != NULL)
	{
		delwin(window);
		window = NULL;
	}
}

/**
 * Accessor to get the window size and position information
 * @return WindowInfo structure with the size and position of the window
 */
WindowInfo Window::GetWindowInfo() const
{
	return windowInfo;
}

/**
 * Moves the window to the new position by the top left corner of the window.
 * @param top Row where the window will be.
 * @param left Column where the window will be.
 */
void Window::SetPosition(unsigned int top, unsigned int left)
{
	// In order to "move" the window, we have to destroy it and recreate it
	// with the new position.  The window info will have to be updated as well.
	if (window != NULL)
	{
		delwin(window);
		window = newwin(windowInfo.height, windowInfo.width, top, left);
		windowInfo.topRow = top;
		windowInfo.leftCol = left;
	}
}

/**
 * Changes everything about the window based on the passed in information.
 * @param info WindowInfo structure with the width, height, and position.
 */
void Window::SetWindow(WindowInfo newInfo)
{
	// In order to "move" and "resize" the window, we have to destroy it and recreate it
	// with the new information.  The window info will have to be updated as well.
	if (window != NULL)
	{
		delwin(window);
		window = newwin(newInfo.height, newInfo.width, newInfo.topRow, newInfo.leftCol);
		windowInfo.height = newInfo.height;
		windowInfo.width = newInfo.width;
		windowInfo.topRow = newInfo.topRow;
		windowInfo.leftCol = newInfo.leftCol;
	}
}
